﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LGE;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using System.IO;

namespace Stealth
{
    public class LevelSelectScreen : GameScreen
    {
        const int nLines = 3;
        const int nColumns = 5;
        String[][] levels = new String[nLines][];
        Vector2 cursorPos = new Vector2(0, 0);

        Texture2D spriteBack;
        Texture2D window;
        Vector2 firstPos = new Vector2(200, 150);
        Vector2 windowSpacing = new Vector2(200, 200);
        Vector2 backPos;


        public LevelSelectScreen()
            : base()
        {
        }

        public override void LoadContent()
        {
            backPos = firstPos + (windowSpacing * new Vector2(-1, nLines - 1));
            window = Engine.Load<Texture2D>("Images\\tile");
            spriteBack = Engine.Load<Texture2D>("Images\\LevelSelect\\back");
            for (int i = 0; i < nLines; ++i)
            {
                levels[i] = new String[nColumns];
                for (int j = 0; j < nColumns; ++j)
                    levels[i][j] = i.ToString();
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (Input.KeyPressed(Keys.Enter))
            {
                Audio.PlayCue("ls_cursor_select");
                if (cursorPos.X == -1) { GameScreenManager.ChangeScreen(new StartScreen()); return; }
                if (File.Exists("Content\\Levels\\" + GetLevelByPos(cursorPos) + ".tmx"))
                    GameScreenManager.ChangeScreen(new PlayScreen(GetLevelByPos(cursorPos)));
            }

            if (Input.KeyPressed(Keys.Left)) { CursorPosAdd(-1, 0); Audio.PlayCue("ls_cursor_move"); }
            if (Input.KeyPressed(Keys.Right)) { CursorPosAdd(1, 0); Audio.PlayCue("ls_cursor_move"); }
            if (Input.KeyPressed(Keys.Up)) { CursorPosAdd(0, -1); Audio.PlayCue("ls_cursor_move"); }
            if (Input.KeyPressed(Keys.Down)) { CursorPosAdd(0, 1); Audio.PlayCue("ls_cursor_move"); }

            if (cursorPos.X == -1)
                cursorPos.Y = nLines - 1;

            if (Input.KeyPressed(Keys.A)) Stealth.Level++;
            if (Input.KeyPressed(Keys.Z)) Stealth.Level--;
        }

        public void CursorPosAdd(int x, int y)
        {
            if (cursorPos.X == -1 && x == 1)
            {
                if (Stealth.Level < 10) cursorPos.Y = 1;
                if (Stealth.Level < 5) cursorPos.Y = 0;
            }
            if (cursorPos.X + x + (nColumns * (cursorPos.Y + y)) > Stealth.Level) return;

            cursorPos += new Vector2(x, y);

            cursorPos.X = MathHelper.Clamp(cursorPos.X, -1, nColumns - 1);
            cursorPos.Y = MathHelper.Clamp(cursorPos.Y, 0, nLines - 1);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(spriteBack, backPos, Color.White);
            DrawWindows(spriteBatch);
            spriteBatch.Draw(window, firstPos + (windowSpacing * cursorPos) + new Vector2(10, 10), Color.White);
            spriteBatch.DrawString(Stealth.Font, "Level: " + GetLevelByPos(cursorPos).ToString()+"/"+Stealth.Level, Vector2.One, Color.White);
        }

        public void DrawWindows(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < nLines; ++i)
                for (int j = 0; j < nColumns; ++j)
                {
                    if (GetLevelByPos(new Vector2(j, i)) > Stealth.Level)
                        return;
                    spriteBatch.Draw(window, firstPos + (windowSpacing * new Vector2(j, i)), Color.White);
                }
        }

        public int GetLevelByPos(Vector2 v)
        {
            return (int)v.X + (nColumns * (int)v.Y);
        }
    }
}